package pl.edu.mimuw.irs.core.model.analysis.resources;

import java.util.LinkedHashSet;
import java.util.Set;

import pl.edu.mimuw.irs.core.global.IDManager;
import pl.edu.mimuw.irs.core.model.design.resource.Requirement;
import pl.edu.mimuw.irs.core.model.petrinet.Place;
import pl.edu.mimuw.irs.core.util.Position;

public class RequirementPlace extends Place {

	private static final long serialVersionUID = 905584428737105972L;
	
	// *** Attributes
	
	private Requirement requirement;
	private Set<ResourcePlace> meetingResources;
	public Requirement getRequirement() {
		return requirement;
	}
	public Set<ResourcePlace> getMeetingResources() {
		if (meetingResources == null) meetingResources = new LinkedHashSet<ResourcePlace>();
		return meetingResources;
	}

	// *** Constructors
	
	public RequirementPlace(IDManager idManager, Requirement requirement, Position position, int tokens) {
		super(idManager, idManager.getPrefixedNewId("Avl" + requirement.getName()), 
				position, tokens);
		this.requirement = requirement;
	}
	
	public RequirementPlace(IDManager idManager, Requirement requirement, Position position) {
		this(idManager, requirement, position, 0);
	}
	
	// *** Methods
	
	@Override
	public void setTokens(int tokens) {
		int oldTokens = getTokens();
		super.setTokens(tokens);
		getListeners().firePropertyChange(TOKENS, oldTokens, tokens);
	}
	
	@Override
	public void addTokens(int num) {
		int oldTokens = getTokens();
		super.addTokens(num);
		getListeners().firePropertyChange(TOKENS, oldTokens, getTokens());
	}
	
	@Override
	public String toString() {
		return "*" + getRequirement() + "*";
	}
}
